package com.guang.executor.createthread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author lg
 * @Description 测试创建100个线程
 * @date 2022/10/24 16:13
 */
public class CreateThreadPoolTestTwo {
    private final static Logger logger = LoggerFactory.getLogger(CreateThreadPoolTestTwo.class);
    public static void main(String[] args) throws InterruptedException {



    }

    /**
     * 使用list来创建100个线程 花费时间 220 ms
     *
     * @throws InterruptedException
     */
    private static void createThreadPoolForList() throws InterruptedException {
        List<Thread> threadPool = new ArrayList<>(100);
        long start = System.currentTimeMillis();
        logger.info("创建线程池开始");
        for (int i = 0; i < 100; i++) {
            Thread thread = new Thread(() -> {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "t" + i);
            thread.start();
            threadPool.add(thread);
            TimeUnit.MICROSECONDS.sleep(1);
        }
        logger.info("创建100个线程的线程池花费时间：{} ms",System.currentTimeMillis()-start);
    }
}
